package com.ramos.kuaixiao.shops.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.MyView.SelfDialog;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.adapter.CartAdapter;
import com.ramos.kuaixiao.db.BrandInfoDB;
import com.ramos.kuaixiao.entity.BrandInfo;
import com.ramos.kuaixiao.entity.CartInfo;
import com.ramos.kuaixiao.ui.SendOrderActivity;
import com.ramos.kuaixiao.utils.CommonLoading;
import com.ramos.kuaixiao.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class MallShopFragment extends Fragment implements OnClickListener {
    private static final String TAG = "MallShopFragment";
    private View mView;
    private Context mContext;
    private ExpandableListView mListView;
    private TextView title_text;
    private TextView title_order;
    public final static int UPLOAD_SUCCESS = 100;
    public final static int UPLOAD_FAIL = 101;
    private CartAdapter mAdapter;
    private List<BrandInfo> tasklist = new ArrayList<>();
    private List<CartInfo> cartlist = new ArrayList<>();
    BrandInfoDB brandInfoDB;

    private TextView title_clean;
    LinearLayout ll_cart_none;
    CommonLoading loading;
    double money;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        container = (ViewGroup) mView.getParent();
        if (container != null) {
            container.removeView(mView);
        }
        return mView;
    }

    public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);

        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");
        initData();
    }

    private void initView() {
        mContext = getActivity();
        mView = LayoutInflater.from(mContext).inflate(R.layout.activity_cart, null);
        title_text = (TextView) mView.findViewById(R.id.title_text);
        title_text.setText("购物车");
        title_order = (TextView) mView.findViewById(R.id.title_order);
        title_order.setOnClickListener(this);
        mListView = (ExpandableListView) mView.findViewById(R.id.pslv_cart_list);
        mListView.setGroupIndicator(null);
        ll_cart_none = (LinearLayout) mView.findViewById(R.id.ll_cart_none);
        title_clean = (TextView) mView.findViewById(R.id.title_clean);
        title_clean.setOnClickListener(this);
        brandInfoDB = new BrandInfoDB();
    }

    class UpdateListener implements CartAdapter.UpdateCarListener {
        @Override
        public void update(List<BrandInfo> mListGoods) {
            showText(mListGoods);
        }
    }

    private void showText(List<BrandInfo> mListGoods) {
        BrandInfoDB user = new BrandInfoDB();
        money = user.getAllMoney(mListGoods);
        if (money == 0) {
            mListView.setVisibility(View.GONE);
            ll_cart_none.setVisibility(View.VISIBLE);
        }
    }

    private void initData() {
        tasklist = brandInfoDB.findAllShop(App.mUserId);
        Log.d(TAG, "onResume " + tasklist.size());
        if (tasklist == null || tasklist.size() == 0) {
            mListView.setVisibility(View.GONE);
            ll_cart_none.setVisibility(View.VISIBLE);
        } else {
            Log.d(TAG, "onResume " + tasklist.size());
            mListView.setVisibility(View.VISIBLE);
            ll_cart_none.setVisibility(View.GONE);
            cartlist = brandInfoDB.getAllCart(tasklist);
            mAdapter = new CartAdapter(mContext, cartlist);
            mAdapter.setUpdateCarListener(new MallShopFragment.UpdateListener());
            mListView.setAdapter(mAdapter);
            int groupCount = mListView.getCount();
            for (int i=0; i<groupCount; i++) {
                mListView.expandGroup(i);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_clean:
                clean();
                break;
            case R.id.title_order:
                //sendOrder();
                newOrder();
            default:
                break;
        }
    }

    private void newOrder() {
        if(tasklist.size() == 0){
            ToastUtils.show("购物车为空，无法下单！");
        }else {
            Intent intent = new Intent(mContext, SendOrderActivity.class);
            startActivity(intent);
        }
    }

    private void clean() {
        final SelfDialog builder = new SelfDialog(getActivity());
        builder.setTitle("删除商品");
        builder.setMessage("是否删除该商品");
        builder.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                builder.dismiss();
            }
        });
        builder.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                brandInfoDB.deleteAllShop();
                mListView.setVisibility(View.GONE);
                //ll_cart_total.setVisibility(View.GONE);
                ll_cart_none.setVisibility(View.VISIBLE);
                builder.dismiss();
            }
        });
        builder.show();

    }
}
